﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ResxCrunch.Properties;
using log4net;
using System.IO;
using ResxCrunch.Business.Managers;
using ResxCrunch.Utils;

namespace ResxCrunch
{
    /// <summary>
    /// Filters form
    /// </summary>
    public partial class FrmFilters : Form
    {
        #region Constants
        /// <summary>
        /// Logger declaration
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #endregion Constants

        #region Attributes
        DataTable filtersTable = new DataTable();
        #endregion Attributes

        /// <summary>
        /// Sets a value indicating whether this <see cref="FrmMain"/> is busy.
        /// </summary>
        /// <value><c>true</c> if busy; otherwise, <c>false</c>.</value>
        public bool Busy
        {
            set
            {
                toolStripFilters.Enabled = !value;
                btnClose.Enabled = !value;

                this.Refresh();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmFilters"/> class.
        /// </summary>
        public FrmFilters()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the FrmFilters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void FrmFilters_Load(object sender, EventArgs e)
        {
            this.LoadFilters();
            this.Enablings();
        }

        private void Enablings()
        {
            toolStripButtonDelete.Enabled = true;

            if (lstFilters.Items.Count == 0)
            {
                toolStripButtonDelete.Enabled = false;
            }
        }

        private void LoadFilters()
        {
            this.Busy = true;

            try
            {
                if (!File.Exists(Settings.Default.FilterFilePath))
                {
                    DataMngr.Instance.CreateEmptyResxCrunchFiltersFile(Settings.Default.FilterFilePath);
                }

                filtersTable.ReadXml(Settings.Default.FilterFilePath);

                lstFilters.DisplayMember = Enums.FilterTableDataColumn.Filter;
                lstFilters.ValueMember = Enums.FilterTableDataColumn.Filter;
                lstFilters.DataSource = filtersTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Filters table could not be loaded. Please verify if the application's install folder is accessible.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                log.Error("Error while loading filters", ex);

                this.Close();
            }

            this.Busy = false;
            this.Enablings();
        }

        #region Events
        /// <summary>
        /// Handles the Click event of the toolStripButtonAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void toolStripButtonAdd_Click(object sender, EventArgs e)
        {
            this.Busy = true;
            try
            {
                FrmAddFilter addFilterForm = new FrmAddFilter();

                if (addFilterForm.ShowDialog() == DialogResult.OK)
                {
                    string filter = addFilterForm.FilterValue;

                    DataRow dr = filtersTable.NewRow();
                    dr[Enums.FilterTableDataColumn.Filter] = filter;
                    filtersTable.Rows.Add(dr);

                    filtersTable.WriteXml(Settings.Default.FilterFilePath, XmlWriteMode.WriteSchema);
                }
            }
            catch (Exception ex)
            {
                log.Error("Could not save filters", ex);
                MessageBox.Show("Error while saving the filters. Please try again.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            this.Busy = false;
            this.Enablings();
        }

        /// <summary>
        /// Handles the Click event of the btnClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            this.Busy = true;

            try
            {
                filtersTable.Rows[lstFilters.SelectedIndex].Delete();
                filtersTable.WriteXml(Settings.Default.FilterFilePath, XmlWriteMode.WriteSchema);
            }
            catch (Exception ex)
            {
                log.Error("Could not save filters", ex);
                MessageBox.Show("Error while saving the filters. Please try again.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            this.Busy = false;
            this.Enablings();
        }
        #endregion Events
    }
}
